package com.zhourong.mongodb.config;

import java.net.UnknownHostException;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import com.mongodb.MongoClientURI;

@Configuration
public class MongoTemplateConfig {

	/**
	 * 
	 * 初始化模板
	 * @param mongoDbFactory
	 * @return
	 */
	
	@Bean
	public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory) {
		MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory);
		return mongoTemplate;
	}
	
	/**
	 * 
	 * 自定义 mongodb factory 配置
	 * @return
	 * @throws UnknownHostException
	 */
	@Bean
    public MongoDbFactory mongoDbFactory() throws UnknownHostException {
        String uriStr="mongodb://127.0.0.1:27017/mongodb";
        System.out.println(uriStr);
        MongoClientURI mongoClientURI=new MongoClientURI(uriStr);
        MongoDbFactory mongoDbFactory=new SimpleMongoDbFactory(mongoClientURI);
        return mongoDbFactory;
    }
}
